package GUILayer;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsConfiguration;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ListSelectionModel;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import CtrlLayer.CtrlLayer;
import CtrlLayer.ItemCtrl;
import CtrlLayer.OrderCtrl;
import CtrlLayer.PersonCtrl;
import GUIadapter.OrderModel;
import GUIadapter.PanelTableModelAdapter;
import ModelLayer.Customer;
import ModelLayer.Employee;
import ModelLayer.PartOrder;
import ModelLayer.SaleItem;
import ModelLayer.SaleOrder;

@SuppressWarnings("serial")
public class PopupOpretOrdre extends PopupWindow implements Updateable
{
	private Customer customer;
	private Employee employee;
	private PersonCtrl pc;
	private CtrlLayer cl;
	private List<SaleOrder> saleorderList;
	private PanelTableModelAdapter orderModel;
	private int indexSaleOrderModel;
	private OrderCtrl orderCtrl;
	private ItemCtrl ic;
	private JTextField txtPhone;
	private JTextField txtName;
	private JTextField txtEmployee;
	JTextField txtOrdernr;
	private PopupTilfoejVare popupTilfoejVare;

	public PopupOpretOrdre(Updateable mother)
	{
		super(mother);
	    cl = new CtrlLayer();
	    pc = new PersonCtrl();
	    ic = new ItemCtrl();
	    popupTilfoejVare = new PopupTilfoejVare(this);
	    
		getContentPane().setPreferredSize(new Dimension(800, 450));
		setTitle("Opret salgsordre");
        
        JButton btnOpretFaktura = new JButton("Opret faktura");
        
        JButton btnGem = new JButton("Gem");
        btnGem.addActionListener(new ActionListener() 
        {
        	public void actionPerformed(ActionEvent e) 
        	{
        		updateSaleOrder();
        		getMother().update();
        		quit();
        	}
        });
        
        btnGem.addActionListener(new ActionListener()
		
		{
			public void actionPerformed(ActionEvent arg0)
			{
				if ((indexSaleOrderModel >= 0) && (indexSaleOrderModel < saleorderList.size()))
				{
					update();
				}
			}
		});
        
        JCheckBox chckbxUdlejning = new JCheckBox("Udlejning");
        
        JCheckBox chckbxSalg = new JCheckBox("Salg");
        chckbxSalg.setSelected(true);
        
        JPanel panel = new JPanel();
        panel.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0), 2), "Kunde", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        
        JPanel panel_1 = new JPanel();
        panel_1.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        
        JPanel panel_2 = new JPanel();
        panel_2.setBorder(new LineBorder(new Color(0, 0, 0), 2));
        
        orderCtrl = new OrderCtrl();

		saleorderList = orderCtrl.getSaleOrderList();

		orderModel = new PanelTableModelAdapter(new OrderModel(saleorderList));
		
		orderModel.setBorder(new TitledBorder(new LineBorder(new Color(0, 0, 0), 2), "Vare(r)", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		orderModel.getTable().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		orderModel.getTable().getSelectionModel().addListSelectionListener(new ListSelectionListener()
		{
			@Override
			public void valueChanged(ListSelectionEvent event)
			{
				if (event.getValueIsAdjusting() == false)
				{
					indexSaleOrderModel = orderModel.getTable()
						.getSelectedRow();
				}
			}
		});
        
        JButton btnTilfoejVare = new JButton("Tilfoej Vare");
        btnTilfoejVare.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent e)
        	{
        		popupTilfoejVare.setVisible(true);
        		popupTilfoejVare.addnr();
        	}
        });
        
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
        	groupLayout.createParallelGroup(Alignment.TRAILING)
        		.addGroup(groupLayout.createSequentialGroup()
        			.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
        				.addGroup(Alignment.TRAILING, groupLayout.createParallelGroup(Alignment.TRAILING)
        					.addGroup(Alignment.LEADING, groupLayout.createSequentialGroup()
        						.addContainerGap()
        						.addComponent(orderModel, GroupLayout.DEFAULT_SIZE, 780, Short.MAX_VALUE))
        					.addGroup(groupLayout.createSequentialGroup()
        						.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
        							.addGroup(groupLayout.createSequentialGroup()
        								.addGap(19)
        								.addComponent(btnOpretFaktura)
        								.addGap(18)
        								.addComponent(btnGem)
        								.addGap(18)
        								.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
        									.addComponent(chckbxSalg)
        									.addComponent(chckbxUdlejning)))
        							.addGroup(groupLayout.createSequentialGroup()
        								.addContainerGap()
        								.addComponent(panel, GroupLayout.PREFERRED_SIZE, 313, GroupLayout.PREFERRED_SIZE)))
        						.addPreferredGap(ComponentPlacement.RELATED, 111, Short.MAX_VALUE)
        						.addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
        							.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 356, GroupLayout.PREFERRED_SIZE)
        							.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 356, GroupLayout.PREFERRED_SIZE))))
        				.addGroup(groupLayout.createSequentialGroup()
        					.addGap(346)
        					.addComponent(btnTilfoejVare)))
        			.addContainerGap())
        );
        groupLayout.setVerticalGroup(
        	groupLayout.createParallelGroup(Alignment.LEADING)
        		.addGroup(groupLayout.createSequentialGroup()
        			.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
        				.addGroup(groupLayout.createSequentialGroup()
        					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
        						.addGroup(groupLayout.createSequentialGroup()
        							.addGap(19)
        							.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
        								.addComponent(btnOpretFaktura)
        								.addComponent(btnGem)))
        						.addGroup(groupLayout.createSequentialGroup()
        							.addContainerGap()
        							.addComponent(chckbxUdlejning)
        							.addPreferredGap(ComponentPlacement.UNRELATED)
        							.addComponent(chckbxSalg)))
        					.addPreferredGap(ComponentPlacement.UNRELATED)
        					.addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        				.addGroup(groupLayout.createSequentialGroup()
        					.addContainerGap()
        					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
        					.addPreferredGap(ComponentPlacement.UNRELATED)
        					.addComponent(panel_2, GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE)))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addComponent(orderModel, GroupLayout.PREFERRED_SIZE, 224, GroupLayout.PREFERRED_SIZE)
        			.addPreferredGap(ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
        			.addComponent(btnTilfoejVare)
        			.addContainerGap())
        );
        
        JLabel lblNewLabel = new JLabel("Saelger: ");
        
        txtEmployee = new JTextField();
        txtEmployee.setBorder(null);
        txtEmployee.setEditable(false);
        txtEmployee.setColumns(10);
        GroupLayout gl_panel_2 = new GroupLayout(panel_2);
        gl_panel_2.setHorizontalGroup(
        	gl_panel_2.createParallelGroup(Alignment.LEADING)
        		.addGroup(gl_panel_2.createSequentialGroup()
        			.addContainerGap()
        			.addComponent(lblNewLabel)
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addComponent(txtEmployee, GroupLayout.DEFAULT_SIZE, 285, Short.MAX_VALUE)
        			.addContainerGap())
        );
        gl_panel_2.setVerticalGroup(
        	gl_panel_2.createParallelGroup(Alignment.LEADING)
        		.addGroup(gl_panel_2.createSequentialGroup()
        			.addContainerGap()
        			.addGroup(gl_panel_2.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblNewLabel, GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE)
        				.addComponent(txtEmployee, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addContainerGap())
        );
        panel_2.setLayout(gl_panel_2);
        
        JLabel lblDato = new JLabel("Dato: " + cl.getDateString());
        lblDato.setFont(new Font("Tahoma", Font.PLAIN, 15));
        
        JLabel lblTid = new JLabel("Tid: " + cl.getDateTimeString());
        lblTid.setFont(new Font("Tahoma", Font.PLAIN, 15));
        
        JLabel lblLeveringsdato = new JLabel("Leveringsdato: ");
        lblLeveringsdato.setFont(new Font("Tahoma", Font.PLAIN, 15));
        
        JLabel lblOrdrenr = new JLabel("Ordrenr.: ");
        lblOrdrenr.setFont(new Font("Tahoma", Font.PLAIN, 15));
        
        JButton btnTilfoejUdlejningsdato = new JButton("Tilfoej leveringsdato");
        
        txtOrdernr = new JTextField();
        txtOrdernr.setFont(new Font("Tahoma", Font.PLAIN, 15));
        txtOrdernr.setBorder(null);
        txtOrdernr.setEditable(false);
        txtOrdernr.setColumns(10);
        
        GroupLayout gl_panel_1 = new GroupLayout(panel_1);
        gl_panel_1.setHorizontalGroup(
        	gl_panel_1.createParallelGroup(Alignment.LEADING)
        		.addGroup(gl_panel_1.createSequentialGroup()
        			.addContainerGap()
        			.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING, false)
        				.addGroup(gl_panel_1.createSequentialGroup()
        					.addComponent(lblDato)
        					.addGap(18)
        					.addComponent(lblTid))
        				.addGroup(gl_panel_1.createSequentialGroup()
        					.addComponent(lblLeveringsdato)
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addComponent(btnTilfoejUdlejningsdato))
        				.addGroup(gl_panel_1.createSequentialGroup()
        					.addComponent(lblOrdrenr)
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addComponent(txtOrdernr)))
        			.addContainerGap(109, Short.MAX_VALUE))
        );
        gl_panel_1.setVerticalGroup(
        	gl_panel_1.createParallelGroup(Alignment.LEADING)
        		.addGroup(gl_panel_1.createSequentialGroup()
        			.addContainerGap()
        			.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblDato)
        				.addComponent(lblTid))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblLeveringsdato)
        				.addComponent(btnTilfoejUdlejningsdato))
        			.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        			.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblOrdrenr)
        				.addComponent(txtOrdernr, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addContainerGap())
        );
        panel_1.setLayout(gl_panel_1);
        
        JPanel pnlSearchCustomer = new JPanel();
        GroupLayout gl_panel = new GroupLayout(panel);
        gl_panel.setHorizontalGroup(
        	gl_panel.createParallelGroup(Alignment.TRAILING)
        		.addGroup(Alignment.LEADING, gl_panel.createSequentialGroup()
        			.addContainerGap()
        			.addComponent(pnlSearchCustomer, GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
        			.addContainerGap())
        );
        gl_panel.setVerticalGroup(
        	gl_panel.createParallelGroup(Alignment.TRAILING)
        		.addGroup(Alignment.LEADING, gl_panel.createSequentialGroup()
        			.addComponent(pnlSearchCustomer, GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
        			.addContainerGap())
        );
        
        JButton btnSearchCustomer = new JButton("Soeg");
        btnSearchCustomer.addActionListener(new ActionListener() {
        	public void actionPerformed(ActionEvent arg0)
        	{
        		addCustomer();
        	}
        });
        
        txtPhone = new JTextField();
        txtPhone.setColumns(10);
        
        JLabel lblKunde = new JLabel("Telefonnr.: ");
        
        JLabel lblNavn = new JLabel("Navn: ");
        
        txtName = new JTextField();
        txtName.setBorder(null);
        txtName.setEditable(false);
        txtName.setColumns(10);
        GroupLayout gl_pnlSearchCustomer = new GroupLayout(pnlSearchCustomer);
        gl_pnlSearchCustomer.setHorizontalGroup(
        	gl_pnlSearchCustomer.createParallelGroup(Alignment.LEADING)
        		.addGroup(gl_pnlSearchCustomer.createSequentialGroup()
        			.addContainerGap()
        			.addGroup(gl_pnlSearchCustomer.createParallelGroup(Alignment.LEADING)
        				.addGroup(gl_pnlSearchCustomer.createSequentialGroup()
        					.addComponent(lblKunde)
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addComponent(txtPhone, GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE))
        				.addGroup(gl_pnlSearchCustomer.createSequentialGroup()
        					.addComponent(lblNavn)
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addComponent(txtName, GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)))
        			.addPreferredGap(ComponentPlacement.UNRELATED)
        			.addComponent(btnSearchCustomer)
        			.addContainerGap())
        );
        gl_pnlSearchCustomer.setVerticalGroup(
        	gl_pnlSearchCustomer.createParallelGroup(Alignment.LEADING)
        		.addGroup(gl_pnlSearchCustomer.createSequentialGroup()
        			.addContainerGap()
        			.addGroup(gl_pnlSearchCustomer.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblKunde)
        				.addComponent(btnSearchCustomer)
        				.addComponent(txtPhone, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        			.addGroup(gl_pnlSearchCustomer.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblNavn)
        				.addComponent(txtName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addContainerGap())
        );
        pnlSearchCustomer.setLayout(gl_pnlSearchCustomer);
        panel.setLayout(gl_panel);
        
        getContentPane().setLayout(groupLayout);
        pack();
	}
	
	public void addCustomer()
	{
		String phone = txtPhone.getText();
        customer = pc.searchCustomer(phone);
        
        if(customer == null)
        {
        	txtName.setText("Kunden eksisterer ikke.");
        }
        else
        {
        	txtName.setText("");
        }
	}
	
	public void addEmployee()
	{
		employee = pc.getFEmployee();
		if(employee == null)
		{
			txtEmployee.setText("Ingen medarbejder");
		}
		else
		{
			txtEmployee.setText(employee.getName());
		}
	}
	
	public void addOrdernr()
	{
		int ordernr = orderCtrl.getLOrder();
        txtOrdernr.setText("" + ordernr);
	}
	
	@Override
	public void update()
	{
		orderModel.getTable().revalidate();
		orderModel.getTable().repaint();
	}
	
	public void updateSaleOrder()
	{
		int ordernr = Integer.parseInt(txtOrdernr.getText());
    	String date = cl.getDateString();
    	double totalPrice = 0;
    	int deliveryDiscount = 0;
    	Employee employee = pc.getFEmployee();
    	String deliveryDate = "Aldrig";
    	String phone = txtPhone.getText();
        customer = pc.searchCustomer(phone);
    	double offer = 0;
    	orderCtrl.updateSO(ordernr, date, totalPrice, employee, deliveryDiscount, customer, deliveryDate, offer);
    	
    	update();
	}
}
